package com.masterclient.app.modules.coupon.adapter;

import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.eoner.baselibrary.utils.TimeUtil;
import com.eoner.baselibrary.utils.Utils;
import com.masterclient.app.R;
import com.masterclient.app.modules.coupon.CouponFragment;
import com.masterclient.app.modules.coupon.beans.NewCouponBean;

import java.text.ParseException;

/**
 * 作者：sean
 * 创建时间：2017/8/4 17:24
 * 功能说明：
 */

public class GetCoupanRecycleAdapter extends BaseQuickAdapter<NewCouponBean.CouponBean, BaseViewHolder> {

    String tag;

    public GetCoupanRecycleAdapter(String tag) {
        super(R.layout.item_get_coupon_goods);
        this.tag = tag;
    }

    @Override
    protected void convert(BaseViewHolder helper, NewCouponBean.CouponBean item) {

        helper.setText(R.id.tv_money_sign, "¥");
        Utils.setDin(helper.getView(R.id.tv_money_sign), mContext);
        helper.setText(R.id.tv_money, item.discountAmount);
        Utils.setDin(helper.getView(R.id.tv_money), mContext);
        helper.setText(R.id.tv_limit, item.name);

        if (helper.getLayoutPosition() == 0){
            helper.setGone(R.id.view_top_line, true);
        }else {
            helper.setGone(R.id.view_top_line, false);
        }

        View ll_by_money = helper.getView(R.id.ll_by_money);
        View right_layout = helper.getView(R.id.right_layout);
        TextView tvMoneySign = helper.getView(R.id.tv_money_sign);
        TextView tvMoney = helper.getView(R.id.tv_money);
        TextView tvLimit = helper.getView(R.id.tv_limit);
        TextView tvMoneyDes = helper.getView(R.id.tv_money_des);
        TextView tvMoneyDiscountSign = helper.getView(R.id.tv_money_discount_sign);
        TextView tvCouponDate = helper.getView(R.id.tv_coupon_date);
        TextView tv_btn = helper.getView(R.id.tv_btn);
        ImageView receivedIcon = helper.getView(R.id.received_icon);
        View viewCouponDown = helper.getView(R.id.view_coupon_down);
        ImageView ivCouponDown = helper.getView(R.id.iv_coupon_down);
        TextView tvCouponMs = helper.getView(R.id.tv_coupon_ms);
        View viewDes = helper.getView(R.id.view_des);
        TextView tvCouponState = helper.getView(R.id.tv_coupon_state);
        CheckBox checkBox = helper.getView(R.id.checkBox);
        checkBox.setVisibility(View.GONE);

        if (!TextUtils.isEmpty(item.detailDesc)){
            tvMoneyDes.setVisibility(View.VISIBLE);
            tvMoneyDes.setText(item.detailDesc);
        }else {
            tvMoneyDes.setVisibility(View.GONE);
        }

        if ("by_fixed".equals(item.simpleAction)) {
            tvMoneySign.setVisibility(View.VISIBLE);
            tvMoneyDiscountSign.setVisibility(View.GONE);
        } else {
            tvMoneySign.setVisibility(View.GONE);
            tvMoneyDiscountSign.setVisibility(View.VISIBLE);
        }


        try {
            tvCouponDate.setText("有效期 "+TimeUtil.formatTime(item.startAt)+"-"+TimeUtil.formatTime(item.endAt));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        tvCouponMs.setText(item.conponDesc);
        tvCouponMs.setVisibility(View.GONE);
        viewDes.setVisibility(View.GONE);

        ivCouponDown.setImageResource(R.mipmap.icon_coupon_down);
        viewCouponDown.setOnClickListener(view -> {
            if (item.isExpanded) {
                item.isExpanded = false;
                ivCouponDown.setImageResource(R.mipmap.icon_coupon_down);
                tvCouponMs.setVisibility(View.GONE);
                viewDes.setVisibility(View.GONE);
                ll_by_money.setBackgroundResource(R.mipmap.bg_uesless_coupon_left);
                right_layout.setBackgroundResource(R.drawable.bg_useless_coupon_right);
            } else {
                item.isExpanded = true;
                ivCouponDown.setImageResource(R.mipmap.icon_coupon_up);
                tvCouponMs.setVisibility(View.VISIBLE);
                viewDes.setVisibility(View.VISIBLE);
                ll_by_money.setBackgroundResource(R.mipmap.bg_uesless_coupon_left_expanded);
                right_layout.setBackgroundResource(R.drawable.bg_useless_coupon_right_expanded);
                tvCouponMs.setBackgroundResource(R.drawable.bg_useless_coupon_down);
            }
        });

        helper.addOnClickListener(R.id.tv_btn);
        if (item.coupon_receive_status){
            tv_btn.setVisibility(View.VISIBLE);
            if ("继续领取".equals(item.coupon_receive_desc)){
                receivedIcon.setVisibility(View.VISIBLE);
                receivedIcon.setImageResource(R.mipmap.icon_coupon_get);
            }else {
                receivedIcon.setVisibility(View.GONE);
            }
        }else {
            tv_btn.setVisibility(View.GONE);
            receivedIcon.setImageResource(R.mipmap.icon_coupon_finish);
            receivedIcon.setVisibility(View.VISIBLE);
        }

        switch (tag) {
            case CouponFragment.UNUSE:
                ll_by_money.setBackgroundResource(R.mipmap.bg_uesless_coupon_left);
                right_layout.setBackgroundResource(R.drawable.bg_useless_coupon_right);
                tvMoneySign.setTextColor(Color.parseColor("#333333"));
                tvMoneyDiscountSign.setTextColor(Color.parseColor("#333333"));
                tvMoney.setTextColor(Color.parseColor("#333333"));
                tvMoneyDes.setTextColor(Color.parseColor("#333333"));
                tvLimit.setTextColor(Color.parseColor("#FF141922"));
                tvCouponDate.setTextSize(9);
                tvCouponDate.setTextColor(Color.parseColor("#FF525866"));
                tvCouponState.setTextColor(Color.parseColor("#ff525866"));
                tvCouponMs.setTextColor(Color.parseColor("#FF525866"));
                break;
            case CouponFragment.USED:
                ll_by_money.setBackgroundResource(R.mipmap.bg_uesless_coupon_left);
                right_layout.setBackgroundResource(R.drawable.bg_useless_coupon_right);
                tvMoneySign.setTextColor(Color.parseColor("#66333333"));
                tvMoneyDiscountSign.setTextColor(Color.parseColor("#66333333"));
                tvMoney.setTextColor(Color.parseColor("#66333333"));
                tvMoneyDes.setTextColor(Color.parseColor("#66333333"));
                tvLimit.setTextColor(Color.parseColor("#FFC2C5CC"));
                tvCouponDate.setTextSize(9);
                tvCouponDate.setTextColor(Color.parseColor("#FFC2C5CC"));
                tvCouponState.setTextColor(Color.parseColor("#FFC2C5CC"));
                tvCouponMs.setTextColor(Color.parseColor("#FF525866"));
                break;
            case CouponFragment.USELESS:
                ll_by_money.setBackgroundResource(R.mipmap.bg_uesless_coupon_left);
                right_layout.setBackgroundResource(R.drawable.bg_useless_coupon_right);
                tvMoneySign.setTextColor(Color.parseColor("#FFC2C5CC"));
                tvMoneyDiscountSign.setTextColor(Color.parseColor("#FFC2C5CC"));
                tvMoney.setTextColor(Color.parseColor("#FFC2C5CC"));
                tvMoneyDes.setTextColor(Color.parseColor("#FFC2C5CC"));
                tvLimit.setTextColor(Color.parseColor("#FFC2C5CC"));
                tvCouponDate.setTextSize(9);
                tvCouponDate.setTextColor(Color.parseColor("#FFC2C5CC"));
                tvCouponState.setTextColor(Color.parseColor("#FFC2C5CC"));
                tvCouponMs.setTextColor(Color.parseColor("#FFC2C5CC"));
                break;
        }

        if (item.isExpanded) {
            ivCouponDown.setImageResource(R.mipmap.icon_coupon_up);
            tvCouponMs.setVisibility(View.VISIBLE);
            viewDes.setVisibility(View.VISIBLE);
            ll_by_money.setBackgroundResource(R.mipmap.bg_uesless_coupon_left_expanded);
            right_layout.setBackgroundResource(R.drawable.bg_useless_coupon_right_expanded);
            tvCouponMs.setBackgroundResource(R.drawable.bg_useless_coupon_down);
        } else {
            ivCouponDown.setImageResource(R.mipmap.icon_coupon_down);
            tvCouponMs.setVisibility(View.GONE);
            viewDes.setVisibility(View.GONE);
            ll_by_money.setBackgroundResource(R.mipmap.bg_uesless_coupon_left);
            right_layout.setBackgroundResource(R.drawable.bg_useless_coupon_right);
        }
    }

}
